package com.common.android.ui;

import com.common.android.utils.NetWorkUtils;
import com.common.android.utils.ToastUtils;
import com.nd.android.u.allcommon.R;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
 * 通用加载基类
 * 
 * <br>Created 2014-10-31 下午2:34:51
 * @version  
 * @author   huangszh		
 *
 * @see 	 
 * 
 * Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
 *
 */
public abstract class CommonLoadingActivity extends Activity{

	/** 加载中 */
	public final int STATE_LOADING = 0;
	/** 加载失败 */
	public final int STATE_LOAD_FAIL = 1;
	/** 加载成功 */
	public final int STATE_LOAD_SUCCESS = 2;
	protected View rootView;
	protected LinearLayout llLoading ;
	protected ProgressBar pbLoading;
	protected TextView tvLoading;
	
	protected LinearLayout llRetry ;
	protected ImageView ivRetry;
	protected TextView tvRetry;
	
	protected TextView tvNoData;
	/** 加载状态 */
	private int mLoadState;
	
	/**
	 * 初始化通用加载布局控件
	 * 
	 * <br>Created 2014-9-4 下午4:20:04
	 * @author       huangszh
	 */
	protected void initComponent(){
		
		rootView = findViewById(R.id.flRoot);
		llLoading = (LinearLayout) findViewById(R.id.llLoading);
		pbLoading = (ProgressBar) findViewById(R.id.pbLoading);
		tvLoading = (TextView) findViewById(R.id.tvLoding);
		
		llRetry = (LinearLayout) findViewById(R.id.llRetry);
		ivRetry = (ImageView) findViewById(R.id.ivRetry);
		tvRetry = (TextView) findViewById(R.id.tvRetry);
		
		tvNoData = (TextView) findViewById(R.id.tvNoData);
		rootView.setOnClickListener(retry);
	}
	
	private OnClickListener retry = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			// 失败状态下点击重试
			if(v.getId() == R.id.flRoot && mLoadState ==  STATE_LOAD_FAIL){
				// 网络不行
				if(!NetWorkUtils.JudgeNetWorkStatus(CommonLoadingActivity.this)){
					ToastUtils.display("网络不给力");
					return;
				}
				showLoadingLayout();
				doOrRetry();
			}
		}
	};
	
	/**
	 * 加载中具体的操作
	 * 
	 * <br>Created 2014-9-4 下午4:20:24
	 * @author       huangszh
	 */
	protected abstract void doOrRetry();
	
	/**
	 * 获取数据时，显示加载布局，隐藏其他
	 * 
	 * <br>Created 2014-9-4 下午4:17:34
	 * @author       huangszh
	 */
	protected void showLoadingLayout(){
		
		mLoadState = STATE_LOADING;
		llLoading.setVisibility(View.VISIBLE);
		llRetry.setVisibility(View.GONE);
	}
	
	/**
	 * 获取到数据后，隐藏加载，错误重试，无数据等布局。
	 * 
	 * <br>Created 2014-9-4 下午4:16:55
	 * @author       huangszh
	 */
	protected void hideAllCommonView(){
		
		mLoadState = STATE_LOAD_SUCCESS;
		rootView.setVisibility(View.GONE);
	}
	
	/**
	 * 获取失败后，显示重试布局，隐藏其他
	 * 
	 * <br>Created 2014-9-4 下午4:17:58
	 * @author       huangszh
	 */
	protected void showRetryLayout(){
		
		mLoadState = STATE_LOAD_FAIL;
		llLoading.setVisibility(View.GONE);
		llRetry.setVisibility(View.VISIBLE);
	}
	
	/**
	 * 获取成功后且无数据，显示无数据布局，隐藏其他
	 * 
	 * <br>Created 2014-9-4 下午4:18:16
	 * @param res 无数据提示语,传null则使用默认提示
	 * @author       huangszh
	 */
	protected void showNoData(String res){
		
		mLoadState = STATE_LOAD_SUCCESS;
		rootView.setVisibility(View.VISIBLE);
		llLoading.setVisibility(View.GONE);
		llRetry.setVisibility(View.GONE);
		tvNoData.setVisibility(View.VISIBLE);
		if(res != null){
			tvNoData.setText(res);
		}
	}
	
	/**
	 * 获取成功后且无数据，显示无数据布局，隐藏其他
	 * 
	 * <br>Created 2014-9-4 下午4:18:58
	 * @param resId 无数据提示语,传null则使用默认提示
	 * @author       huangszh
	 */
	protected void showNoData(int resId){
		
		if(resId != 0){
			showNoData(getString(resId));
		} else{
			showNoData(null);
		}
	}
}
